// pages/mine/mine.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
    avatarUrl -> 头像地址
    nickName -> 昵称
    sex -> 性别
    changeValue  判断该页面是否可以操作对
   */

  data: {
    avatarUrl:"",
    nickName:"",
    sex:"男",
    username: "",
    phoneNum: "",
    changeValue: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  页面渲染时候，得到用户的头像 昵称 性别 姓名 手机号
    //100% 肯定会有userId
    let _this = this
    let uid = (wx.getStorageSync("userId"))
    if(uid){
      // uid 存在时候 
      console.log("uid 存在")
      let userInfo = wx.getStorageSync("userInfo");
      let avatarUrl = userInfo.avatarUrl;
      let nickName = userInfo.nickName;
      //录入性别
      let sex = ""
      if(userInfo.gender == 1){
        sex="男"
      }else if(userInfo.gender == 2){
        sex="女"
      }
      _this.setData({
        avatarUrl:avatarUrl,
        nickName:nickName,
        sex:sex
      })
    }
    // 获取用户手机号姓名信息
    wx.request({
      url: app.globalData.url+'zreosz', //仅为示例，并非真实的接口地址
      data: {
        uid: uid
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log("zreosz 获取用户手机号姓名信息",res.data)
        if (res.data.data.name) {
          _this.setData({
            'username': res.data.data.name
          })
        }
        if (res.data.data.mobile) {
          _this.setData({
            'phoneNum': res.data.data.mobile
          })
        }
      }
    })
  },
  //姓名输入框移除之后保存起来。
  loseName:function(e){
    let nameValue = e.detail.value
    this.setData({
        'username':e.detail.value,
        'changeValue':true
    })
  }, 
  //电话输入框移除之后保存起来。
  losePhone:function(e){
    let phoneValue = e.detail.value
    this.setData({
        'phoneNum':e.detail.value,
        'changeValue':true
    })
  },
  // 保存按钮
  modification:function(){
    // let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    let uid = (wx.getStorageSync("userId"))
    let _this = this
    let phoneNum = _this.data.phoneNum
    let username = _this.data.username

    if(!this.data.changeValue){
      return;
    }else if(username.length == 0){
      wx.showToast({
        title: '请输入您的称呼',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(!phoneNum.match(myreg)){
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
      wx.request({
        url: app.globalData.url+'zreosz_post', //存
        data: {
          "uid": uid,
          "mobile":phoneNum,
          "name":username
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          wx.request({
            url: app.globalData.url+'zreosz', //显示手机号  姓名
            data: {
              uid: uid
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res.data)
              if(res.data.data.mobile){
                _this.setData({
                  'phoneNum': res.data.data.mobile
                })
                console.log(_this.data.phoneNum)
              }
              if(res.data.data.name){
                _this.setData({
                  'username': res.data.data.name
                })
              }

              wx.showToast({
                title: '信息修改成功',
                icon: 'none',
                mask:true,
                duration: 2000
              })

              _this.setData({
                "changeValue":false
              })
            }
          })
        }
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let uid = wx.getStorageSync("userId")
    let userInfo = wx.getStorageSync("userInfo")
    return {
      title:'您的好友('+userInfo.nickName+')给您推荐一家超赞的家居装修公司!',
      path: '/pages/index/index?shareMan='+uid,
      success: function(res) {
        // 转发成功
        console.log("shareAppMessage: ok")
        console.log(res);
      },
      fail: function(res) {
        // 转发失败
        console.log(res);
      }
    }
  },
    //底部导航条
      //跳转0元
      nav_zero: function () {
        wx.navigateTo({
          url: '../devise/devise'
        })
      },
      //跳转礼物
      nav_gift: function () {
        wx.navigateTo({
          url: '../recommend/recommend'
        })
      },
      //跳转我的
      nav_mine:function(){
        console.log("这已经是我的页面了，所以不跳")
      }
  //底部导航条 END
})